/*
 * author      : 吴中勤 
 * date        : 2019/3/21
 * description : TODO
 */

package org.wzq.learn.infrastructure.lib.trace;

import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class FeignClientInterceptor implements RequestInterceptor {

    private static final Logger logger = LoggerFactory.getLogger(FeignClientInterceptor.class);

    @Override
    public void apply(RequestTemplate requestTemplate) {
        if (logger.isInfoEnabled()) logger.info("FeignClientInterceptor, threadId=" + Thread.currentThread().getId());

        // 添加请求头 x-request-id
        String xRequestId = CustomThreadContext.getXRequestId();
        if (StringUtils.isNotEmpty(xRequestId)) {
            requestTemplate.header("x-request-id", xRequestId);
        }

        // isMock
        boolean isMock = CustomThreadContext.isMock();
        if (isMock) {
            requestTemplate.header("mock", "1");
        }
    }
}